package tv.huan.web.controller.tencent;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tv.huan.common.annotation.Log;
import tv.huan.common.core.controller.BaseController;
import tv.huan.common.core.domain.AjaxResult;
import tv.huan.common.core.page.TableDataInfo;
import tv.huan.common.enums.BusinessType;
import tv.huan.common.utils.poi.ExcelUtil;
import tv.huan.tencent.domain.CardBuyLog;
import tv.huan.tencent.domain.CardPool;
import tv.huan.tencent.service.ICardBuyLogService;
import tv.huan.tencent.service.ICardPoolService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 购买记录Controller
 * 
 * @author admin
 * @date 2020-12-23
 */
@Controller
@RequestMapping("/tencent/cardBuyLog")
public class CardBuyLogController extends BaseController
{
    private String prefix = "tencent/cardBuyLog";

    @Autowired
    private ICardBuyLogService cardBuyLogService;

    @Autowired
    private ICardPoolService cardPoolService;

    @RequiresPermissions("tencent:cardBuyLog:view")
    @GetMapping()
    public String cardBuyLog(ModelMap mmap)
    {
        List<CardPool> cardPoolList = cardPoolService.selectCardPoolList(null);
        mmap.put("cardPoolList",cardPoolList);
        mmap.put("cardPool",new CardPool());
        return prefix + "/cardBuyLog";
    }

    /**
     * 查询购买记录列表
     */
    @RequiresPermissions("tencent:cardBuyLog:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CardBuyLog cardBuyLog)
    {
        startPage();
        List<CardBuyLog> list = cardBuyLogService.selectCardBuyLogList(cardBuyLog);
        return getDataTable(list);
    }

    /**
     * 导出购买记录列表
     */
    @RequiresPermissions("tencent:cardBuyLog:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CardBuyLog cardBuyLog)
    {
        List<CardBuyLog> list = cardBuyLogService.selectCardBuyLogList(cardBuyLog);
        ExcelUtil<CardBuyLog> util = new ExcelUtil<CardBuyLog>(CardBuyLog.class);
        return util.exportExcel(list, "cardBuyLog");
    }

    /**
     * 新增购买记录
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<CardPool> cardPoolList = cardPoolService.selectCardPoolList(null);
        mmap.put("cardPoolList",cardPoolList);
        mmap.put("cardPool",new CardPool());
        return prefix + "/add";
    }

    /**
     * 新增保存购买记录
     */
    @RequiresPermissions("tencent:cardBuyLog:add")
    @Log(title = "购买记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CardBuyLog cardBuyLog)
    {
        if (null != cardBuyLog.getBatchValidityTime() && !isValid(cardBuyLog.getBatchValidityTime())){
                return AjaxResult.error("添加失败，有效期无效");
        }else {
            return toAjax(cardBuyLogService.insertCardBuyLog(cardBuyLog));
        }
    }

    //校验购买记录是否处于有效期
    private boolean isValid(Date validTime){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date now = format.parse(format.format(new Date()));
            if (validTime.after(now)){
                return true;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 修改购买记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        List<CardPool> cardPoolList = cardPoolService.selectCardPoolList(null);
        mmap.put("cardPoolList",cardPoolList);
        CardBuyLog cardBuyLog = cardBuyLogService.selectCardBuyLogById(id);
        mmap.put("cardBuyLog", cardBuyLog);
        return prefix + "/edit";
    }

    /**
     * 修改保存购买记录
     */
    @RequiresPermissions("tencent:cardBuyLog:edit")
    @Log(title = "购买记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CardBuyLog cardBuyLog)
    {
        if (null != cardBuyLog.getBatchValidityTime() && !isValid(cardBuyLog.getBatchValidityTime())){
            return AjaxResult.error("保存失败，有效期无效");
        }else {
            return toAjax(cardBuyLogService.updateCardBuyLog(cardBuyLog));
        }
    }

    /**
     * 删除购买记录
     */
    @RequiresPermissions("tencent:cardBuyLog:remove")
    @Log(title = "购买记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cardBuyLogService.deleteCardBuyLogByIds(ids));
    }
}
